﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Threading;

using HostPc;

namespace HostPc
{
    namespace HVGSerial
    {

        class Controller
        {
            public Adapter hvg_adapter = null;
            public HostPc.Configuration hostpc_config = null;
            
            /**
             * HVGSerial_Controller构造函数**/
            public Controller(ref HostPc.Configuration __hostpc_config)
            {
                hvg_adapter = new Adapter(ref __hostpc_config);

            } // Controller()


            /**
             * 将awsiot的参数传入**/
            public void PassAWSIOTCoreController(ref AWSIOTCoreSDK.Controller awsiot_controller_object)
            {
                // 传入MqttContactAwsIOT_Controller实例化对象。
                hvg_adapter.awsiot_controller = awsiot_controller_object;

            } // PassAWSIOTCoreController()


            /**
             * 开始运行HVGSerial对象**/
            public void Run()
            {

                // 若没有连接上AWSIOT或者没有连接上HVG，则退出
                if (!hvg_adapter.awsiot_controller.awsiot_adapter.mqtt_client.IsConnected)
                {
                    hvg_adapter.awsiot_controller.awsiot_adapter.ReConnectAWSIOTCore();
                }
                if (!hvg_adapter.hvg_serial_port.IsOpen)
                {
                   hvg_adapter.ReconnectHVGSerial();
                }

                // 开始异步监听RS232串口
                hvg_adapter.hvg_serial_port.DataReceived += new SerialDataReceivedEventHandler(hvg_adapter.ReceivedHVGDataHandler);

                // 检测当前HVG是否连接串口
                Thread detect_hvg_connect = new Thread(() => {
                    hvg_adapter.DetectHVGConnected();
                });
                detect_hvg_connect.Start();

            } // Run()

        } // class Controller


    } // HVGSerial

} // HostPc

